import { createRouter, createWebHistory } from 'vue-router'
import Layout from '../layout/index.vue'
import Login from '../views/login/index.vue'

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/register/index.vue')
  },
  {
    path: '/home',
    component: Layout,
    redirect: '/home/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        meta: { title: '首页', requiresAuth: true }
      },
      // 宠物管理
      {
        path: 'pet/list',
        name: 'PetList',
        component: () => import('@/views/pet/list.vue'),
        meta: { title: '宠物列表', requiresAuth: true }
      },
      {
        path: 'pet/category',
        name: 'PetCategory',
        component: () => import('@/views/pet/category.vue'),
        meta: { title: '宠物分类', requiresAuth: true }
      },
      // 商品管理
      {
        path: 'product/list',
        name: 'ProductList',
        component: () => import('@/views/product/list.vue'),
        meta: { title: '商品列表', requiresAuth: true }
      },
      {
        path: 'product/category',
        name: 'ProductCategory',
        component: () => import('@/views/product/category.vue'),
        meta: { title: '商品分类', requiresAuth: true }
      },
      {
        path: 'product/stock',
        name: 'ProductStock',
        component: () => import('@/views/product/stock.vue'),
        meta: { title: '库存管理', requiresAuth: true }
      },
      // 订单管理
      {
        path: 'order/list',
        name: 'OrderList',
        component: () => import('@/views/order/list.vue'),
        meta: { title: '订单列表', requiresAuth: true }
      },
      {
        path: 'order/delivery',
        name: 'OrderDelivery',
        component: () => import('@/views/order/delivery.vue'),
        meta: { title: '发货管理', requiresAuth: true }
      },
      {
        path: 'order/return',
        name: 'OrderReturn',
        component: () => import('@/views/order/return.vue'),
        meta: { title: '退货管理', requiresAuth: true }
      },
      // 领养管理
      {
        path: 'adoption/apply',
        name: 'AdoptionApply',
        component: () => import('@/views/adoption/apply.vue'),
        meta: { title: '领养申请', requiresAuth: true }
      },
      {
        path: 'adoption/record',
        name: 'AdoptionRecord',
        component: () => import('@/views/adoption/record.vue'),
        meta: { title: '领养记录', requiresAuth: true }
      },
      // 投票管理
      {
        path: 'vote/active',
        name: 'VoteActive',
        component: () => import('@/views/vote/active.vue'),
        meta: { title: '进行中投票', requiresAuth: true }
      },
      {
        path: 'vote/history',
        name: 'VoteHistory',
        component: () => import('@/views/vote/history.vue'),
        meta: { title: '历史投票', requiresAuth: true }
      },
      {
        path: 'vote/statistics',
        name: 'VoteStatistics',
        component: () => import('@/views/vote/statistics.vue'),
        meta: { title: '投票统计', requiresAuth: true }
      },
      // 咨询管理
      {
        path: 'consultation/list',
        name: 'ConsultationList',
        component: () => import('@/views/consultation/list.vue'),
        meta: { title: '咨询列表', requiresAuth: true }
      },
      {
        path: 'consultation/reply',
        name: 'ConsultationReply',
        component: () => import('@/views/consultation/reply.vue'),
        meta: { title: '回复管理', requiresAuth: true }
      },
      // 系统管理 - 只保留用户和角色管理
      {
        path: 'system/user',
        name: 'SystemUser',
        component: () => import('@/views/system/user.vue'),
        meta: { title: '用户管理', requiresAuth: true }
      },
      {
        path: 'system/role',
        name: 'SystemRole',
        component: () => import('@/views/system/role.vue'),
        meta: { title: '角色管理', requiresAuth: true }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  
  // 如果是访问登录页
  if (to.path === '/login') {
    if (token) {
      next('/home/dashboard')
    } else {
      next()
    }
    return
  }
  
  // 如果没有token，除了登录页其他页面都重定向到登录页
  if (!token) {
    next({ path: '/login', query: { redirect: to.fullPath } })
    return
  }
  
  // 有token，正常访问
  next()
})

export default router